﻿using System;
using System.Collections.Generic;

using Gonte.Xml.Serialization;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyUtilitiesTest
{
    /// <summary>
    ///This is returnedAttributes test class for XmlObjectDeserializerTest and is intended
    ///to contain all XmlObjectDeserializerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class XmlObjectDeserializerTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in returnedAttributes class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        #region Primitives

        [TestMethod()]
        public void DeserializePrimitiveString()
        {
            String obj = String.Empty;
            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<String>Jorge</String>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            obj = (String)deserializer.DeserializeFromString(xml);

            Assert.AreEqual("Jorge", obj);
        }

        [TestMethod()]
        public void DeserializePrimitiveGuid()
        {
            Guid obj = new Guid();
            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<Guid>5daa64c2-2c28-47d7-86d4-0dc378a0568b</Guid>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            obj = (Guid)deserializer.DeserializeFromString(xml);

            Assert.AreEqual(new Guid("5daa64c2-2c28-47d7-86d4-0dc378a0568b"), obj);
        }

        [TestMethod()]
        public void DeserializePrimitiveDateTime()
        {
            DateTime obj = new DateTime();
            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<DateTime>5/25/2005 2:30:18 AM</DateTime>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            obj = (DateTime)deserializer.DeserializeFromString(xml);

            Assert.AreEqual(new DateTime(2005, 5, 25, 2, 30, 18), obj);
        }

        [TestMethod()]
        public void DeserializePrimitiveEnumeration()
        {
            EnumerationTest obj = EnumerationTest.EnumItem2;
            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<EnumerationTest>EnumItem2</EnumerationTest>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            obj = (EnumerationTest)deserializer.DeserializeFromString(xml);

            Assert.AreEqual(EnumerationTest.EnumItem2, obj);
        } 

        #endregion

        #region Objects

        [TestMethod()]
        public void SerializeWithRootAttribute()
        {
            SimpleObjectWithRootAttribute obj = new SimpleObjectWithRootAttribute();

            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<MyObject Name=""MyName"" />";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual("MyName", obj.Name);
        }

        [TestMethod()]
        public void DeserializeObjectWithPrimitives()
        {
            ObjectWithPrimitivesTest obj = new ObjectWithPrimitivesTest();
            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<ObjectWithPrimitivesTest StringProp=""My string property"" GuidProp=""3b72d25f-0a74-47a3-ae3d-c223af92ec8b"" DatetimeProp=""5/25/2002 2:30:18 AM"" DecimalProp=""125000.43"" DoubleProp=""0.005"" IntProp=""65"" />";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual(obj.GuidProp, new Guid("3b72d25f-0a74-47a3-ae3d-c223af92ec8b"));
            Assert.AreEqual(obj.DatetimeProp, new DateTime(2002, 5, 25, 2, 30, 18));
            Assert.AreEqual(obj.DecimalProp, 125000.43M);
            Assert.AreEqual(obj.DoubleProp, 0.005);
            Assert.AreEqual(obj.IntProp, 65);
            Assert.AreEqual(obj.StringProp, "My string property");
        }

        [TestMethod()]
        public void DeserializeObjectWithInnerObject()
        {
            ObjectWithInnerObjectAndPrimitivesTest obj = new ObjectWithInnerObjectAndPrimitivesTest();

            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<ObjectWithInnerObjectAndPrimitivesTest StringProp=""Another string property"" IntProp=""23432"">
  <InnerObject StringProp=""My string property"" GuidProp=""3b72d25f-0a74-47a3-ae3d-c223af92ec8b"" DatetimeProp=""5/25/2002 2:30:18 AM"" DecimalProp=""125000.43"" DoubleProp=""0.005"" IntProp=""65"" />
</ObjectWithInnerObjectAndPrimitivesTest>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual(obj.StringProp, "Another string property");
            Assert.AreEqual(obj.IntProp, 23432);

            Assert.AreEqual(obj.InnerObject.GuidProp, new Guid("3b72d25f-0a74-47a3-ae3d-c223af92ec8b"));
            Assert.AreEqual(obj.InnerObject.DatetimeProp, new DateTime(2002, 5, 25, 2, 30, 18));
            Assert.AreEqual(obj.InnerObject.DecimalProp, 125000.43M);
            Assert.AreEqual(obj.InnerObject.DoubleProp, 0.005);
            Assert.AreEqual(obj.InnerObject.IntProp, 65);
            Assert.AreEqual(obj.InnerObject.StringProp, "My string property");
        } 

        #endregion

        [TestMethod()]
        public void DeserializeListOfStrings()
        {
            List<String> obj = new List<String>();
            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<List>
  <String>Toyota</String>
  <String>Honda</String>
  <String>Hyundai</String>
  <String>Acura</String>
</List>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual(4, obj.Count);
            Assert.AreEqual("Toyota", obj[0]);
            Assert.AreEqual("Honda", obj[1]);
            Assert.AreEqual("Hyundai", obj[2]);
            Assert.AreEqual("Acura", obj[3]);
        }

        [TestMethod()]
        public void DeserializeListOfComplexObjects()
        {
            List<ObjectWithInnerObjectAndPrimitivesTest> obj = new List<ObjectWithInnerObjectAndPrimitivesTest>();

            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<List>
  <ObjectWithInnerObjectAndPrimitivesTest StringProp=""Another string property"" IntProp=""23432"">
    <InnerObject StringProp=""My string property"" GuidProp=""3b72d25f-0a74-47a3-ae3d-c223af92ec8b"" DatetimeProp=""5/25/2002 2:30:18 AM"" DecimalProp=""125000.43"" DoubleProp=""0.005"" IntProp=""65"" />
  </ObjectWithInnerObjectAndPrimitivesTest>
  <ObjectWithInnerObjectAndPrimitivesTest StringProp=""Another string property2"" IntProp=""23432"">
    <InnerObject StringProp=""My string property2"" GuidProp=""4b72d25f-0a74-47a3-ae3d-c223af92ec8b"" DatetimeProp=""6/25/2003 2:30:18 AM"" DecimalProp=""225000.43"" DoubleProp=""0.025"" IntProp=""651"" />
  </ObjectWithInnerObjectAndPrimitivesTest>
  <ObjectWithInnerObjectAndPrimitivesTest StringProp=""Another string property3"" IntProp=""23432"">
    <InnerObject StringProp=""My string property3"" GuidProp=""4b72d25f-0a74-47a3-ae3d-c223af92ec8b"" DatetimeProp=""7/25/2004 2:30:18 AM"" DecimalProp=""325000.43"" DoubleProp=""0.035"" IntProp=""652"" />
  </ObjectWithInnerObjectAndPrimitivesTest>
</List>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            ObjectWithInnerObjectAndPrimitivesTest item1 = obj[0];
            Assert.AreEqual(item1.StringProp,  "Another string property");
            Assert.AreEqual(item1.IntProp,  23432);

            Assert.AreEqual(item1.InnerObject.GuidProp,  new Guid("3b72d25f-0a74-47a3-ae3d-c223af92ec8b"));
            Assert.AreEqual(item1.InnerObject.DatetimeProp,  new DateTime(2002, 5, 25, 2, 30, 18));
            Assert.AreEqual(item1.InnerObject.DecimalProp,  125000.43M);
            Assert.AreEqual(item1.InnerObject.DoubleProp,  0.005);
            Assert.AreEqual(item1.InnerObject.IntProp,  65);
            Assert.AreEqual(item1.InnerObject.StringProp,  "My string property");

            ObjectWithInnerObjectAndPrimitivesTest item2 = obj[1];
            Assert.AreEqual(item2.StringProp,  "Another string property2");
            Assert.AreEqual(item2.IntProp,  23432);

            Assert.AreEqual(item2.InnerObject.GuidProp,  new Guid("4b72d25f-0a74-47a3-ae3d-c223af92ec8b"));
            Assert.AreEqual(item2.InnerObject.DatetimeProp,  new DateTime(2003, 6, 25, 2, 30, 18));
            Assert.AreEqual(item2.InnerObject.DecimalProp,  225000.43M);
            Assert.AreEqual(item2.InnerObject.DoubleProp,  0.025);
            Assert.AreEqual(item2.InnerObject.IntProp,  651);
            Assert.AreEqual(item2.InnerObject.StringProp,  "My string property2");

            ObjectWithInnerObjectAndPrimitivesTest item3 = obj[2];
            Assert.AreEqual(item3.StringProp,  "Another string property3");
            Assert.AreEqual(item3.IntProp,  23432);

            Assert.AreEqual(item3.InnerObject.GuidProp,  new Guid("4b72d25f-0a74-47a3-ae3d-c223af92ec8b"));
            Assert.AreEqual(item3.InnerObject.DatetimeProp,  new DateTime(2004, 7, 25, 2, 30, 18));
            Assert.AreEqual(item3.InnerObject.DecimalProp,  325000.43M);
            Assert.AreEqual(item3.InnerObject.DoubleProp,  0.035);
            Assert.AreEqual(item3.InnerObject.IntProp,  652);
            Assert.AreEqual(item3.InnerObject.StringProp,  "My string property3");

        }

        [TestMethod()]
        public void DeserializeListOfListsOfStrings()
        {
            List<List<String>> obj = new List<List<String>>();
            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<List>
  <List>
    <String>Ford</String>
    <String>Chevrolet</String>
    <String>Dodge</String>
  </List>
  <List>
    <String>Toyota</String>
    <String>Honda</String>
    <String>Acura</String>
  </List>
  <List>
    <String>Hyundai</String>
    <String>Kia</String>
  </List>
</List>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual(obj[0][0], "Ford");
            Assert.AreEqual(obj[0][1], "Chevrolet");
            Assert.AreEqual(obj[0][2], "Dodge");

            Assert.AreEqual(obj[1][0], "Toyota");
            Assert.AreEqual(obj[1][1], "Honda");
            Assert.AreEqual(obj[1][2], "Acura");

            Assert.AreEqual(obj[2][0], "Hyundai");
            Assert.AreEqual(obj[2][1], "Kia");
        }

        /// <summary>
        /// Tests calling a non default constructor during deserialization
        /// </summary>
        [TestMethod()]
        public void DeserializeSimpleParentWithChild()
        {
            SimpleParentObject parent = new SimpleParentObject();
            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<SimpleParentObject Name=""TheParent"">
  <Children>
    <SimpleChildObject Name=""Child1"" />
    <SimpleChildObject Name=""Child2"" />
  </Children>
</SimpleParentObject>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(parent);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual("TheParent", parent.Name);

            Assert.AreEqual("Child1", parent.Children[0].Name);
            Assert.AreEqual(parent, parent.Children[0].Parent);

            Assert.AreEqual("Child2", parent.Children[1].Name);
            Assert.AreEqual(parent, parent.Children[1].Parent);
        }

        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void SerializeObjectMissingDictionaryItemAttribute()
        {
            ObjectMissingDictionaryItemAttribute obj = new ObjectMissingDictionaryItemAttribute();
            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<ObjectMissingDictionaryItemAttribute>
  <Dictionary>
    <Dictionary>A</Dictionary>
    <Dictionary>B</Dictionary>
    <Dictionary>C</Dictionary>
  </Dictionary>
</ObjectMissingDictionaryItemAttribute>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            //Assert.AreEqual("A", obj.Dictionary["A"]);
            //Assert.AreEqual("B", obj.Dictionary["B"]);
            //Assert.AreEqual("C", obj.Dictionary["C"]);
            
        }

        [TestMethod()]
        public void DeserializeObjectWithStringKeyDictionaryOfStrings()
        {
            ObjectWithStringKeyDictionaryOfStrings obj = new ObjectWithStringKeyDictionaryOfStrings();
            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<ObjectWithStringKeyDictionaryOfStrings>
  <Values>
    <Values Key=""1"">A</Values>
    <Values Key=""2"">B</Values>
    <Values Key=""3"">C</Values>
  </Values>
</ObjectWithStringKeyDictionaryOfStrings>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual("A", obj.Values["1"]);
            Assert.AreEqual("B", obj.Values["2"]);
            Assert.AreEqual("C", obj.Values["3"]);
        }

        [TestMethod()]
        public void DeserializeSimpleObjectWithCDataTest()
        {
            SampleObject obj = new SampleObject();
            String xml =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<SampleObject>
    <Name>A</Name>
    <Data>
    <![CDATA[
    B
    ]]>
    </Data>
</SampleObject>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual("A", obj.Name);
            Assert.AreEqual("B", obj.Data);
        }

        #region Extra

        [TestMethod()]
        public void DeserializeTestSingleTextNode()
        {
            NotTooComplexTestObject obj = new NotTooComplexTestObject();
            String xml =
                @"<NotTooComplexTestObject Name=""myName"" Class=""myClass"" Number=""1234567"">
                    MyInnerObject
                </NotTooComplexTestObject>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual("myName", obj.Name);
            Assert.AreEqual("myClass", obj.Class);
            Assert.AreEqual(1234567, obj.Number);
            Assert.AreEqual("MyInnerObject", obj.Text.Trim());
        }

        /// <summary>
        ///A test for DeserializeFromString (object, XmlNode)
        ///</summary>
        [TestMethod()]
        public void DeserializeTestSingleInnerObject()
        {
            NotTooComplexTestObject obj = new NotTooComplexTestObject();
            String xml =
                @"<NotTooComplexTestObject Name=""myName"" Class=""myClass"" Number=""1234567"">
                    <InnerObject>MyInnerObject</InnerObject>
                </NotTooComplexTestObject>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual("myName", obj.Name);
            Assert.AreEqual("myClass", obj.Class);
            Assert.AreEqual(1234567, obj.Number);
            Assert.AreEqual("MyInnerObject", obj.InnerObject.Trim());
        }

        /// <summary>
        ///A test for DeserializeFromString (object, XmlNode)
        ///</summary>
        [TestMethod()]
        public void DeserializeTestDifferentInnerObjects()
        {
            NotTooComplexTestObject obj = new NotTooComplexTestObject();
            String xml =
                @"<NotTooComplexTestObject Name=""myName"" Class=""myClass"" Number=""1234567"">
                    <InnerObject>MyInnerObject</InnerObject>
                    <InnerObjectWithProperties Prop1=""prop1"" Prop2=""prop2""></InnerObjectWithProperties>
                </NotTooComplexTestObject>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual("myName", obj.Name);
            Assert.AreEqual("myClass", obj.Class);
            Assert.AreEqual(1234567, obj.Number);
            Assert.AreEqual("MyInnerObject", obj.InnerObject.Trim());

            Assert.AreEqual("prop1", obj.InnerObjectWithProperties.Prop1);
            Assert.AreEqual("prop2", obj.InnerObjectWithProperties.Prop2);
        }

        /// <summary>
        ///A test for DeserializeFromString (object, XmlNode)
        ///</summary>
        [TestMethod()]
        public void DeserializeTestDifferentInnerObjectName()
        {
            NotTooComplexTestObject obj = new NotTooComplexTestObject();
            String xml =
                @"<NotTooComplexTestObject Name=""myName"" Class=""myClass"" Number=""1234567"">
                    <Inner>MyInnerObject</Inner>
                    <InnerObjectWithProperties Prop1=""prop1"" Prop2=""prop2""></InnerObjectWithProperties>
                </NotTooComplexTestObject>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual("myName", obj.Name);
            Assert.AreEqual("myClass", obj.Class);
            Assert.AreEqual(1234567, obj.Number);
            Assert.AreEqual("MyInnerObject", obj.InnerObject.Trim());

            Assert.AreEqual("prop1", obj.InnerObjectWithProperties.Prop1);
            Assert.AreEqual("prop2", obj.InnerObjectWithProperties.Prop2);
        }

        /// <summary>
        ///A test for DeserializeFromString (object, XmlNode)
        ///</summary>
        [TestMethod()]
        public void DeserializeTestCollectionStrings()
        {
            NotTooComplexTestObject obj = new NotTooComplexTestObject();
            String xml =
                @"<NotTooComplexTestObject Name=""myName"" Class=""myClass"" Number=""1234567"">
                    <InnerObject>MyInnerObject</InnerObject>
                    <InnerObjectWithProperties Prop1=""prop1"" Prop2=""prop2""></InnerObjectWithProperties>
                    <Names>
                        <Name>Sarah</Name>
                        <Name>Yana</Name>
                        <Name>Mark</Name>
                        <Name>Jorge</Name>
                    </Names>
                </NotTooComplexTestObject>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual("myName", obj.Name);
            Assert.AreEqual("myClass", obj.Class);
            Assert.AreEqual(1234567, obj.Number);
            Assert.AreEqual("MyInnerObject", obj.InnerObject.Trim());

            Assert.AreEqual("prop1", obj.InnerObjectWithProperties.Prop1);
            Assert.AreEqual("prop2", obj.InnerObjectWithProperties.Prop2);

            Assert.AreEqual(4, obj.Names.Count);
            Assert.AreEqual("Sarah", obj.Names[0]);
            Assert.AreEqual("Yana", obj.Names[1]);
            Assert.AreEqual("Mark", obj.Names[2]);
            Assert.AreEqual("Jorge", obj.Names[3]);
        }

        /// <summary>
        ///A test for DeserializeFromString (object, XmlNode)
        ///</summary>
        [TestMethod()]
        public void DeserializeTestCollectionObjects()
        {
            NotTooComplexTestObject obj = new NotTooComplexTestObject();
            String xml =
                @"<NotTooComplexTestObject Name=""myName"" Class=""myClass"" Number=""1234567"">
                    <InnerObject>MyInnerObject</InnerObject>
                    <InnerObjectWithProperties Prop1=""prop1"" Prop2=""prop2""></InnerObjectWithProperties>
                    <Names>
                        <Name>Sarah</Name>
                        <Name>Yana</Name>
                        <Name>Mark</Name>
                        <Name>Jorge</Name>
                    </Names>
                    <SampleObjects>
                        <SampleObject Name=""name1"" Data=""data1""/>
                        <SampleObject Name=""name2"" Data=""data2""/>
                        <SampleObject Name=""name3"" Data=""data3""/>
                        <SampleObject Name=""name4"" Data=""data4""/>
                        <SampleObject Name=""name5"" Data=""data5""/>
                    </SampleObjects>
                </NotTooComplexTestObject>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual("myName", obj.Name);
            Assert.AreEqual("myClass", obj.Class);
            Assert.AreEqual(1234567, obj.Number);
            Assert.AreEqual("MyInnerObject", obj.InnerObject.Trim());

            Assert.AreEqual("prop1", obj.InnerObjectWithProperties.Prop1);
            Assert.AreEqual("prop2", obj.InnerObjectWithProperties.Prop2);

            Assert.AreEqual(4, obj.Names.Count);
            Assert.AreEqual("Sarah", obj.Names[0]);
            Assert.AreEqual("Yana", obj.Names[1]);
            Assert.AreEqual("Mark", obj.Names[2]);
            Assert.AreEqual("Jorge", obj.Names[3]);

            Assert.AreEqual(5, obj.SampleObjects.Count);
        }

        /// <summary>
        ///A test for DeserializeFromString (object, XmlNode)
        ///</summary>
        [TestMethod()]
        public void DeserializeTestDictionary()
        {
            PersisterInfo obj = new PersisterInfo();
            String xml =
                @"<PersisterInfo Name=""myName"" ConnectionName=""myClass"" >
                    <PredefinedCommand Type=""Text"">
                        SELECT * FROM PREDEFINED
                    </PredefinedCommand>
                    <Commands>
                        <CommandInfo Name=""Command1"">
                            SELECT * FROM TABLE1
                        </CommandInfo>
                        <CommandInfo Name=""Command2"">
                            SELECT * FROM TABLE2
                        </CommandInfo>
                        <CommandInfo Name=""Command3"">
                            SELECT * FROM TABLE3
                        </CommandInfo>
                    </Commands>
                </PersisterInfo>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual(obj.Commands.Count, 4);
            Assert.AreEqual("SELECT * FROM PREDEFINED", obj.Commands["Predefined"].Text);
            Assert.AreEqual("SELECT * FROM TABLE1", obj.Commands["Command1"].Text);
            Assert.AreEqual("SELECT * FROM TABLE2", obj.Commands["Command2"].Text);
            Assert.AreEqual("SELECT * FROM TABLE3", obj.Commands["Command3"].Text);
        }

        /// <summary>
        ///A test for DeserializeFromString (object, XmlNode)
        ///</summary>
        //        [TestMethod()]
        //        public void DeserializeTestDictionaryWithoutCommandsNode()
        //        {
        //            PersisterInfo obj = new PersisterInfo();
        //            String xml =
        //                @"<PersisterInfo Name=""myName"" ConnectionName=""myClass"" >
        //
        //                    <PredefinedCommand FieldType=""Text"">
        //                        <Text>SELECT * FROM PREDEFINED</Text>
        //                    </PredefinedCommand>
        //
        //                    <PostdefinedCommand FieldType=""Text"">
        //                        <Text>SELECT * FROM POSTDEFINED</Text>
        //                    </PostdefinedCommand>
        //
        //                    <CommandInfo Name=""Command1""  FieldType=""Text"">
        //                        <Text>SELECT * FROM TABLE1</Text>
        //                    </CommandInfo>
        //
        //                    <CommandInfo Name=""Command2""  FieldType=""Text"">
        //                        <Text>SELECT * FROM TABLE2</Text>
        //                    </CommandInfo>
        //
        //                    <CommandInfo Name=""Command3""  FieldType=""Text"">
        //                        <Text>SELECT * FROM TABLE3</Text>
        //                    </CommandInfo>
        //
        //                </PersisterInfo>";

        //            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
        //            deserializer.DeserializeFromString(xml);

        //            Assert.AreEqual(5, obj.Commands.Count);
        //            Assert.AreEqual("SELECT * FROM PREDEFINED", obj.Commands["Predefined"].Text);
        //            Assert.AreEqual("Text", obj.Commands["Predefined"].FieldType);
        //            Assert.AreEqual("SELECT * FROM POSTDEFINED", obj.Commands["Postdefined"].Text);
        //            Assert.AreEqual("Text", obj.Commands["Postdefined"].FieldType);
        //            Assert.AreEqual("SELECT * FROM TABLE1", obj.Commands["Command1"].Text);
        //            Assert.AreEqual("Text",obj.Commands["Command1"].FieldType);
        //            Assert.AreEqual("SELECT * FROM TABLE2", obj.Commands["Command2"].Text);
        //            Assert.AreEqual("SELECT * FROM TABLE3", obj.Commands["Command3"].Text);
        //        }

        /// <summary>
        ///A test for DeserializeFromString (object, XmlNode)
        ///</summary>
        [TestMethod()]
        public void DeserializeTestOutOfPositionDictionaryWithoutCommandsNode()
        {
            PersisterInfo obj = new PersisterInfo();
            String xml =
                @"<PersisterInfo Name=""myName"" ConnectionName=""myClass"" >

                    <ObjectWithArray>

                        <SampleObject name=""name1"" data=""data1""/>
                        <SampleObject name=""name2"" data=""data2""/>
                        <SampleObject name=""name3"" data=""data3""/>
                        <SampleObject name=""name4"" data=""data4""/>
                        <SampleObject name=""name5"" data=""data5""/>

                    </ObjectWithArray>

                    <PredefinedCommand Type=""Text"">
                        <Text>SELECT * FROM PREDEFINED</Text>
                    </PredefinedCommand>

                    <PostdefinedCommand Type=""Text"">
                        <Text>SELECT * FROM POSTDEFINED</Text>
                    </PostdefinedCommand>

                    <CommandInfo Name=""Command1""  Type=""Text"">
                        <Text>SELECT * FROM TABLE1</Text>
                    </CommandInfo>

                    <CommandInfo Name=""Command2""  Type=""Text"">
                        <Text>SELECT * FROM TABLE2</Text>
                    </CommandInfo>

                    <CommandInfo Name=""Command3""  Type=""Text"">
                        <Text>SELECT * FROM TABLE3</Text>
                    </CommandInfo>

                </PersisterInfo>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual(5, obj.ObjectWithArray.SampleObjects.Length);
            Assert.AreEqual("name1", obj.ObjectWithArray.SampleObjects[0].Name);
            Assert.AreEqual("data1", obj.ObjectWithArray.SampleObjects[0].Data);
            Assert.AreEqual("name5", obj.ObjectWithArray.SampleObjects[4].Name);
            Assert.AreEqual("data5", obj.ObjectWithArray.SampleObjects[4].Data);

            Assert.AreEqual(5, obj.Commands.Count);
            Assert.AreEqual("SELECT * FROM PREDEFINED", obj.Commands["Predefined"].Text);
            Assert.AreEqual("Text", obj.Commands["Predefined"].Type);
            Assert.AreEqual("SELECT * FROM POSTDEFINED", obj.Commands["Postdefined"].Text);
            Assert.AreEqual("Text", obj.Commands["Postdefined"].Type);
            Assert.AreEqual("SELECT * FROM TABLE1", obj.Commands["Command1"].Text);
            Assert.AreEqual("Text", obj.Commands["Command1"].Type);
            Assert.AreEqual("SELECT * FROM TABLE2", obj.Commands["Command2"].Text);
            Assert.AreEqual("SELECT * FROM TABLE3", obj.Commands["Command3"].Text);
        }

        /// <summary>
        ///A test for DeserializeFromString (object, XmlNode)
        ///</summary>
        [TestMethod()]
        public void DeserializeTestStringKeyDictionaryWithoutCommandsNode()
        {
            PersisterInfo2 obj = new PersisterInfo2();
            String xml =
                @"<PersisterInfo Name=""myName"" ConnectionName=""myClass"" >

                    <PredefinedCommand Type=""Text"">
                        <Text>SELECT * FROM PREDEFINED</Text>
                    </PredefinedCommand>

                    <PostdefinedCommand Type=""Text"">
                        <Text>SELECT * FROM POSTDEFINED</Text>
                    </PostdefinedCommand>

                    <CommandInfo Name=""Command1""  Type=""Text"">
                        <Text>SELECT * FROM TABLE1</Text>
                    </CommandInfo>

                    <CommandInfo Name=""Command2""  Type=""Text"">
                        <Text>SELECT * FROM TABLE2</Text>
                    </CommandInfo>

                    <CommandInfo Name=""Command3""  Type=""Text"">
                        <Text>SELECT * FROM TABLE3</Text>
                    </CommandInfo>

                </PersisterInfo>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual(5, obj.Commands.Count);
            Assert.AreEqual("SELECT * FROM PREDEFINED", obj.Commands["Predefined"].Text);
            Assert.AreEqual("Text", obj.Commands["Predefined"].Type);
            Assert.AreEqual("SELECT * FROM POSTDEFINED", obj.Commands["Postdefined"].Text);
            Assert.AreEqual("Text", obj.Commands["Postdefined"].Type);
            Assert.AreEqual("SELECT * FROM TABLE1", obj.Commands["Command1"].Text);
            Assert.AreEqual("Text", obj.Commands["Command1"].Type);
            Assert.AreEqual("SELECT * FROM TABLE2", obj.Commands["Command2"].Text);
            Assert.AreEqual("SELECT * FROM TABLE3", obj.Commands["Command3"].Text);
        }

        /// <summary>
        ///A test for DeserializeFromString (object, XmlNode)
        ///</summary>
        [TestMethod()]
        public void DeserializeObjectWithArrayTest()
        {
            ObjectWithArray obj = new ObjectWithArray();
            String xml =
                @"<ObjectWithArray>
                    <SampleObjects>
                        <SampleObject Name=""name1"" Data=""data1""/>
                        <SampleObject Name=""name2"" Data=""data2""/>
                        <SampleObject Name=""name3"" Data=""data3""/>
                        <SampleObject Name=""name4"" Data=""data4""/>
                        <SampleObject Name=""name5"" Data=""data5""/>
                    </SampleObjects>
                 </ObjectWithArray>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual(5, obj.SampleObjects.Length);
            Assert.AreEqual("name1", obj.SampleObjects[0].Name);
            Assert.AreEqual("data1", obj.SampleObjects[0].Data);
            Assert.AreEqual("name5", obj.SampleObjects[4].Name);
            Assert.AreEqual("data5", obj.SampleObjects[4].Data);
        }

        [TestMethod()]
        public void DeserializeObjectWithArrayOutOfPositionTest()
        {
            ObjectWithArray obj = new ObjectWithArray();
            String xml =
                @"<ObjectWithArray>
                        <SampleObject name=""name1"" data=""data1""/>
                        <SampleObject name=""name2"" data=""data2""/>
                        <SampleObject name=""name3"" data=""data3""/>
                        <SampleObject name=""name4"" data=""data4""/>
                        <SampleObject name=""name5"" data=""data5""/>
                 </ObjectWithArray>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual(5, obj.SampleObjects.Length);
            Assert.AreEqual("name1", obj.SampleObjects[0].Name);
            Assert.AreEqual("data1", obj.SampleObjects[0].Data);
            Assert.AreEqual("name5", obj.SampleObjects[4].Name);
            Assert.AreEqual("data5", obj.SampleObjects[4].Data);
        }

        [TestMethod()]
        public void DeserializeObjectWithIgnoredXmlAttributesTest()
        {
            ObjectWithIgnoredXmlAttributes obj = new ObjectWithIgnoredXmlAttributes();

            String xml =
                @"<ObjectWithIgnoredXmlAttributes Name=""MyName"">
                 </ObjectWithIgnoredXmlAttributes>";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.IsNull(obj.Name);
        }

        [TestMethod()]
        public void DeserializeObjectWithEnumerationPropertyTest()
        {
            ObjectWithEnumerationProperty obj = new ObjectWithEnumerationProperty();

            String xml =
@"<?xml version=""1.0""?>
<ObjectWithEnumerationProperty WeekDay=""Saturday"" />";

            XmlObjectDeserializer deserializer = XmlObjectDeserializer.GetDeserializer(obj);
            deserializer.DeserializeFromString(xml);

            Assert.AreEqual(ObjectWithEnumerationProperty.WeekDays.Saturday, obj.WeekDay);
        }

        #endregion
    }
}
